package com.springboot.blog.controller;

import com.springboot.blog.beans.AdminUser;
import com.springboot.blog.beans.Blog;
import com.springboot.blog.beans.BlogLog;
import com.springboot.blog.config.utils.IpUtil;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.LogDao;
import com.springboot.blog.service.AdminUserServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

@Controller
@RestController
@RequestMapping("/admin")
public class AdminUserController {

    @Resource
    private AdminUserServiceImpl adminUserService;
    @Resource
    private LogDao logDao;

    @RequestMapping("login")
    public R login(HttpServletRequest request, HttpServletResponse response, String userName, String userPassword, Boolean rememberMe){
        try {
            adminUserService.checkLogin(userName,userPassword,rememberMe);
             if(true) {
                Cookie ckUsername = new Cookie("nms_username",userName);
                ckUsername.setMaxAge(60*60*24*30);
                response.addCookie(ckUsername);
                Cookie ckPassword = new Cookie("nms_password", userPassword);
                ckPassword.setMaxAge(60*60*24*30);
                response.addCookie(ckPassword);
                Cookie ckRemember = new Cookie("nms_remember", "1");
                ckRemember.setMaxAge(60*60*24*30);
                response.addCookie(ckRemember);
            }else {
                Cookie[] cookies = request.getCookies();
                if(cookies != null) {
                    for(Cookie cookie : cookies) {
                        if("nms_username".equals(cookie.getName()) || "nms_password".equals(cookie.getName())
                                || "nms_remember".equals(cookie.getName())) {
                            cookie.setMaxAge(0);
                            response.addCookie(cookie);
                        }
                    }
                }
            }
            BlogLog log = new BlogLog();
             log.setLogname(userName);
             log.setLogip(IpUtil.getIpAddr(request));
            logDao.insert(log);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }
    @RequestMapping("regist")
    public R regist(String userName, String userPassword,String userPhone,String nickName){
        //加盐加密
        int num = new Random().nextInt(90000)+10000;
        System.out.println("num =" + num);
        Md5Hash md5Hash = new Md5Hash(userPassword,num+"");
        adminUserService.addAdminUser(userName,md5Hash.toHex(),userPhone,nickName,String.valueOf(num));
        return R.ok();
    }

    @RequestMapping("findAdminUser")
    public R findAdminUser(HttpServletRequest request,HttpServletResponse response, String userName, String userPhone){
        AdminUser admin = adminUserService.findAdmin(userName, userPhone);
        if (admin == null){
            return R.error();
        }
        request.getSession().setAttribute("userName",admin.getBlogUsername());
        request.getSession().setAttribute("userSession","userSession") ;
        return R.ok();
    }

    @RequestMapping("updateAdminUser")
    public R updateAdminUser(HttpServletRequest request,String userPassword){
        String  userName = (String) request.getSession().getAttribute("userName");
        if (userName != null){
            //加盐加密
            int num = new Random().nextInt(90000)+10000;
            Md5Hash md5Hash = new Md5Hash(userPassword,num+"");
            Boolean updateAdminUser = adminUserService.updateAdminUser(userName, md5Hash.toHex(),String.valueOf(num));
            if (updateAdminUser){
                return R.ok();
            }
        }
        return R.error();
    }

    @RequestMapping(value = "/test")
    public String test(HttpServletRequest request){
        //获取IP地址
        String ipAddress = IpUtil.getIpAddr(request);
        return ipAddress;
    }
}
